using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class OrderForm_MaterialDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

		public OrderForm_MaterialDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the OrderForm_Material table.
		/// </summary>
		public void Insert(OrderForm_MaterialDTO orderForm_Material)
		{
			ValidationUtility.ValidateArgument("orderForm_Material", orderForm_Material);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idorderform", orderForm_Material.Idorderform),
				new SqlParameter("@idmaterial", orderForm_Material.Idmaterial),
				new SqlParameter("@amount", orderForm_Material.Amount),
				new SqlParameter("@pricebyday", orderForm_Material.Pricebyday),
				new SqlParameter("@remaining", orderForm_Material.Remaining)
			};

			orderForm_Material.Id = (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "OrderForm_MaterialInsert", parameters);
		}

		/// <summary>
		/// Updates a record in the OrderForm_Material table.
		/// </summary>
		public void Update(OrderForm_MaterialDTO orderForm_Material)
		{
			ValidationUtility.ValidateArgument("orderForm_Material", orderForm_Material);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", orderForm_Material.Id),
				new SqlParameter("@idorderform", orderForm_Material.Idorderform),
				new SqlParameter("@idmaterial", orderForm_Material.Idmaterial),
				new SqlParameter("@amount", orderForm_Material.Amount),
				new SqlParameter("@pricebyday", orderForm_Material.Pricebyday),
				new SqlParameter("@remaining", orderForm_Material.Remaining)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "OrderForm_MaterialUpdate", parameters);
		}

		/// <summary>
		/// Deletes a record from the OrderForm_Material table by its primary key.
		/// </summary>
		public void Delete(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "OrderForm_MaterialDelete", parameters);
		}

		/// <summary>
		/// Deletes a record from the OrderForm_Material table by a foreign key.
		/// </summary>
		public void DeleteAllByIdmaterial(int idmaterial)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idmaterial", idmaterial)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialDeleteAllByIdmaterial", parameters);
		}

		/// <summary>
		/// Deletes a record from the OrderForm_Material table by a foreign key.
		/// </summary>
		public void DeleteAllByIdorderform(int idorderform)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idorderform", idorderform)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialDeleteAllByIdorderform", parameters);
		}

		/// <summary>
		/// Selects a single record from the OrderForm_Material table.
		/// </summary>
		public OrderForm_MaterialDTO Select(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "OrderForm_MaterialSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects a single record from the OrderForm_Material table.
		/// </summary>
		public string SelectJson(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "OrderForm_MaterialSelect", parameters);
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table.
		/// </summary>
		public List<OrderForm_MaterialDTO> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "OrderForm_MaterialSelectAll"))
			{
				List<OrderForm_MaterialDTO> orderForm_MaterialDTOList = new List<OrderForm_MaterialDTO>();
				while (dataReader.Read())
				{
					OrderForm_MaterialDTO orderForm_MaterialDTO = MapDataReader(dataReader);
					orderForm_MaterialDTOList.Add(orderForm_MaterialDTO);
				}

				return orderForm_MaterialDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table.
		/// </summary>
		public string SelectAllJson()
		{
			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "OrderForm_MaterialSelectAll");
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table by a foreign key.
		/// </summary>
		public List<OrderForm_MaterialDTO> SelectAllByIdmaterial(int idmaterial)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idmaterial", idmaterial)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelectAllByIdmaterial", parameters))
			{
				List<OrderForm_MaterialDTO> orderForm_MaterialDTOList = new List<OrderForm_MaterialDTO>();
				while (dataReader.Read())
				{
					OrderForm_MaterialDTO orderForm_MaterialDTO = MapDataReader(dataReader);
					orderForm_MaterialDTOList.Add(orderForm_MaterialDTO);
				}

				return orderForm_MaterialDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table by a foreign key.
		/// </summary>
		public List<OrderForm_MaterialDTO> SelectAllByIdorderform(int idorderform)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idorderform", idorderform)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelectAllByIdorderform", parameters))
			{
				List<OrderForm_MaterialDTO> orderForm_MaterialDTOList = new List<OrderForm_MaterialDTO>();
				while (dataReader.Read())
				{
					OrderForm_MaterialDTO orderForm_MaterialDTO = MapDataReader(dataReader);
					orderForm_MaterialDTOList.Add(orderForm_MaterialDTO);
				}

				return orderForm_MaterialDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table by a foreign key.
		/// </summary>
		public string SelectAllByIdmaterialJson(int idmaterial)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idmaterial", idmaterial)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelectAllByIdmaterial", parameters);
		}

		/// <summary>
		/// Selects all records from the OrderForm_Material table by a foreign key.
		/// </summary>
		public string SelectAllByIdorderformJson(int idorderform)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idorderform", idorderform)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_OrderForm_MaterialSelectAllByIdorderform", parameters);
		}

		/// <summary>
		/// Creates a new instance of the OrderForm_MaterialDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private OrderForm_MaterialDTO MapDataReader(SqlDataReader dataReader)
		{
			OrderForm_MaterialDTO orderForm_MaterialDTO = new OrderForm_MaterialDTO();
			orderForm_MaterialDTO.Id = dataReader.GetInt32("id", 0);
			orderForm_MaterialDTO.Idorderform = dataReader.GetInt32("idorderform", 0);
			orderForm_MaterialDTO.Idmaterial = dataReader.GetInt32("idmaterial", 0);
			orderForm_MaterialDTO.Amount = dataReader.GetInt32("amount", 0);
			orderForm_MaterialDTO.Pricebyday = dataReader.GetInt32("pricebyday", 0);
			orderForm_MaterialDTO.Remaining = dataReader.GetInt32("remaining", 0);

			return orderForm_MaterialDTO;
		}

		#endregion
	}
}
